import { ref } from "vue"
import { savePlanDetail } from "../../service/matching/";
import { copyText } from "../../utils/copyText"

export function usePlanAtlasHook(props: any, emits: any, generatePlanImg: any) {
  const planData = ref(props.data);
  const showSaveBtn = ref(false);
  const isHandle = ref(false);

  const savePlan = async () => {
    try {
      // build params for request
      const { companyID, keyword, price } = props.params;
      const params = {
        id: props.planID,
        companyId: companyID,
        companyName: keyword,
        revenueScale: price,
        content: JSON.stringify(planData.value.children || [])
      };

      // send request
      const result = await savePlanDetail(params);
      isHandle.value = true
      showSaveBtn.value = false

      console.log();
      if (result.code == 200) {
        ElNotification({
          title: "保存成功",
          message: "可在我的规划中查看",
          type: "success"
        });
      } else {
        ElNotification({
          title: "保存失败",
          message: "请联系管理员处理",
          type: "error"
        });
      }

    } catch (error) {
      ElNotification({
        title: "保存失败",
        message: "请联系管理员处理",
        type: "error"
      });
    }
  };

  const notSave = () => {
    ElMessageBox.confirm(
      "此操作会删除此次匹配结果，是否仍要继续此操作？",
      "警告",
      {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true
      }
    ).then(() => {
      planData.value = null;
      isHandle.value = true
      emits("resetPlanData");
    });
  };

  // share fn
  const shareDialog = ref(false)
  const shareLink = ref(window.location.href)
  const sharePlan = () => {
    shareDialog.value = !shareDialog.value
  }
  const copyLink = () => {
    copyText(shareLink.value)
  }

  // generate pdf file for plan data
  const pdfDrawer = ref(false)
  const generatePdf = () => {
    pdfDrawer.value = !pdfDrawer.value
    generatePlanImg()
  }
  return { savePlan, notSave, planData, showSaveBtn, sharePlan, shareLink, generatePdf, copyLink, shareDialog, pdfDrawer, isHandle }
}
